goog.provide('tipfy.ui.dialogs');

goog.require('tipfy');
goog.require('tipfy.XhrIo');

goog.require('goog.events');
goog.require('goog.ui.Dialog');

/**
 * Dialog to confirm action. Retrieves html content through ajax call.
 *
 * Adapted from CloudCourse.
 *
 * @param {string} url Url to use to render html content.
 * @param {string} title Title of the dialog.
 * @param {string} opt_content Post data. If available, will use POST instead of
 *          GET.
 * @export
 */
tipfy.ui.dialogs.confirmAjaxDialog = function(url, title, opt_content) {
    var method = 'GET';
    if(opt_content) {
        method = 'POST';
    }
    // XXX: add loading indicator
    // url, opt_callback, opt_method, opt_content, opt_headers, opt_timeoutInterval
    tipfy.XhrIo.send(url, function() {
        var body = this.getResponseJson().body,
            dlg = new goog.ui.Dialog();

        dlg.setDisposeOnHide(true);
        dlg.setContent(body);
        dlg.setTitle(title);
        dlg.setButtonSet(goog.ui.Dialog.ButtonSet.OK_CANCEL);
        goog.events.listen(dlg, goog.ui.Dialog.SELECT_EVENT, function(e) {
            if (e.key == 'ok') {
                document.getElementById('dialog_form').submit();
            }
        });
        dlg.setVisible(true);
    }, method, opt_content);
};
